home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / mint / utils / stlogin4.lzh / TEE.C < prev    next >
C/C++ Source or Header  |  1993-07-02  |  2KB  |  93 lines

  1. /* UNIX like tee program. Kees Lemmens; Dec. 1992
  2.  
  3.    This program can be used to duplicate your session to an other file
  4.    or device on a multitasking ATARI ST (MINT). It can not work on a
  5.    normal ATARI, because it needs two separate processes !
  6.    It behaves just like the UNIX tee utility.
  7.  
  8.    Bugs/remarks:
  9. -  Some programs - like tcsh - send a signal TTOU when a foreign process
  10.    attempts to write to their tty. This causes the foreign process to be
  11.    suspended. To avoid this problem the tee program has to ignore such
  12.    signals.
  13. -  Stderr is not copied to the other screen and also terminal sessions
  14.    by means of kermit or tip cannot be duplicated !!
  15.  
  16.    Any questions or suggestions about this program can be send to:
  17.    lemmens@dv.twi.tudelft.nl
  18. */
  19.  
  20. #include <stdio.h>
  21. #include <fcntl.h>    /* O_CREAT etc. */
  22. #include <stdlib.h>
  23. #include <signal.h>
  24. #include "ux_misc.h"
  25.  
  26. void fatal(char *txt)
  27. {    fputs("TEE: ",stderr);
  28.     fputs(txt,stderr);
  29.     exit(1);
  30. }
  31. /*
  32. void do_write(char *output,char *mode)
  33. {    FILE *dupout;
  34.     char c;
  35.  
  36.     if((dupout=fopen(output,mode)) < 0)
  37.         fatal("Can't open file");
  38.  
  39.     setbuf(stdin,NULL);
  40.     setbuf(dupout,NULL);
  41.     while((c=fgetc(stdin)), !feof(stdin))
  42.     {    fputc(c,stdout);
  43.         fputc(c,dupout);
  44.     }
  45.     fclose(dupout);
  46. }
  47. */
  48. void do_write(char *output,int mode)
  49. {    int dupout,hstdin,hstdout;
  50.     char c,cr='\15';
  51.  
  52.     if((dupout=open(output,O_WRONLY|mode)) < 0)
  53.         fatal("Can't open file");
  54.     hstdin=fileno(stdin);
  55.     hstdout=fileno(stdout);
  56.     while(read(hstdin,&c,1) > 0) /* until eof */
  57.     {    write(hstdout,&c,1);
  58.         write( dupout,&c,1);
  59.         if(c == '\n')
  60.         {    write(hstdout,&cr,1);
  61.             write( dupout,&cr,1);
  62.         }
  63.     }
  64.     close(dupout);
  65. }
  66.  
  67. void usage(void)
  68. {    puts("\nUsage: tee [-a] <file|device>");
  69.     exit(1);
  70. }
  71.  
  72. void main(int argc,char *argv[]) 
  73. {    int mode=O_CREAT|O_TRUNC;
  74.  
  75.     if(argc<2)    usage();
  76.  
  77.     /* ignore signal [Attempted write to foreign tty] */
  78.  
  79.     signal(SIGTTOU,SIG_IGN);
  80.  
  81.     while(--argc>0)            /* parse options */
  82.     {    if(*argv[1]=='-')
  83.         {    switch(*(++argv[1]))
  84.             {    case 'a':    mode = O_APPEND;    break;
  85.                 default :    usage();
  86.             }
  87.             ++argv;
  88.         }
  89.     }    
  90.     ux2dos(argv[1]);    /* convert slashes */
  91.     do_write(argv[1],mode);
  92. }
  93.